<!-- 问诊分类 -->
<template>
  <div>
    <ContainerSlot>
      <template slot="searchModule">
        <SearchForm :label-width="labelWidth" :form-config="formConfig" :form-data="formData" :slot-list="slotList">
          <template slot="searchBtn">
            <el-button type="primary" @click.prevent="search()">查询</el-button>
          </template>
        </SearchForm>
      </template>
      <template slot="operationModule">
        <el-button type="primary" plain @click="add">添加</el-button>
        <!-- <el-button type="success" plain @click="del">删除</el-button> -->
      </template>
      <template slot="contentModule">
        <Table :prop-list="propList" :list-data="tableData">
          <template slot="operation" slot-scope="{row}">
            <!-- <el-link v-show="!row.$index" style="color: #55AAFF" @click="edit(row)">修改</el-link>
            <el-link v-show="!row.$index" style="color: red; marginLeft: '50rpx';" @click="del(row)">删除</el-link> -->
            <el-button v-show="row.invitationTypeId" class="opt" type="primary" plain @click="edit(row)">
              <i class="el-icon-edit-outline" />
              编辑
            </el-button>
            <el-button v-show="row.invitationTypeId" class="opt" type="danger" plain @click="del(row)">
              <i class="el-icon-delete" />
              删除
            </el-button>
          </template>
        </Table>
      </template>
      <template slot="footerModule">
        <el-pagination :current-page="currentPage" background :page-size="pageSize" :page-sizes="[10, 20, 30]" layout=" prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </template>
    </ContainerSlot>
    <!-- 对话框 -->
    <el-dialog :close-on-click-modal='false' :title="`${title}分类`" :visible.sync="dialogFormVisible" top="30vh" @close="cancel">
      <el-form ref="ruleForm" :inline="true" :model="form" :rules="rules">
        <el-form-item label="问诊分类名称：" :label-width="formLabelWidth" prop="classification" autocomplete="off">
          <el-input v-model="form.classification" />
        </el-form-item>
        <el-form-item label="备注：" :label-width="formLabelWidth" prop="desc">
          <el-input v-model="form.desc" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ContainerSlot from '@/components/Container'
import SearchForm from '@/components/Form'
import Table from '@/components/Table'
import inquiryClassificationApi from '@/api/specialist-consultation/inquiry-management/inquiry-classification/index'

export default {
  name: '',
  components: { ContainerSlot, SearchForm, Table },
  data() {
    var validateClassification = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('问诊分类名称不能为空'))
      } else {
        callback()
      }
    }
    var validateDesc = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('备注不能为空'))
      } else {
        callback()
      }
    }
    return {
      title: '添加',
      total: 1,
      currentPage: 1,
      pageSize: 10,
      propList: [
        { prop: 'name', label: '问诊分类名称', minWidth: '80' },
        { prop: 'remakes', label: '备注' },
        { label: '操作', slotName: 'operation' }
      ],
      tableData: [],
      labelWidth: '100px',
      // 表单配置
      formConfig: [
        {
          label: '关键词',
          prop: 'name1',
          type: 'input',
          width: '150px'
        }
      ],
      // 查询按钮或重置
      slotList: [
        {
          slotName: 'searchBtn'
        }
      ],
      // 表单数据
      formData: {
        name1: ''
      },
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {
        classification: '',
        desc: ''
      },
      invitationTypeId: '',
      rules: {
        classification: [
          { required: true, message: '请输入问诊分类名称', trigger: 'blur' },
          { validator: validateClassification, trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { validator: validateDesc, trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getClassificationList()
  },
  methods: {
    // 获取帖子分类列表
    async getClassificationList(pages = 1, name = '') {
      if (pages === 0) {
        pages = 1
      }
      const params = {
        page: pages,
        size: this.pageSize,
        condition: {
          name
        }
      }
      const res = await inquiryClassificationApi.queryPageClassification(params)
      if (res.code === 200) {
        this.tableData = res.data.content
        this.total = res.data.totalElements
      }
    },
    handleCurrentChange(e) {
      this.currentPage = e
      this.getClassificationList(e)
    },
    handleSizeChange(e) {
      this.pageSize = e
      this.getClassificationList()
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const params = {}
          const res = await inquiryClassificationApi.delClassification(params, row.invitationTypeId)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            const arr = this.tableData.filter(it => it.invitationTypeId)
            this.getClassificationList(arr.length > 1 ? this.currentPage : this.currentPage - 1)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加
    add() {
      this.dialogFormVisible = true
      this.reset()
      this.title = '添加'
      this.invitationTypeId = ''
    },
    edit(row) {
      this.invitationTypeId = row.invitationTypeId
      this.form.classification = row.name
      this.form.desc = row.remakes
      this.dialogFormVisible = true
      this.title = '修改'
    },
    async confirmAdd() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          // 不存在id-添加
          if (!this.invitationTypeId) {
            const params = {
              name: this.form.classification,
              remakes: this.form.desc
            }
            const res = await inquiryClassificationApi.addClassification(params)
            if (res.code === 200) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.getClassificationList()
              this.dialogFormVisible = false
            }
          } else {
            const params = {
              name: this.form.classification,
              remakes: this.form.desc,
              invitationTypeId: this.invitationTypeId
            }
            const res = await inquiryClassificationApi.editClassification(params)
            if (res.code === 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            } else {
              this.$message({
                message: '修改失败',
                type: 'error'
              })
            }
            this.getClassificationList()
            this.dialogFormVisible = false
            console.log('修改', res)
          }
        } else {
          return false
        }
      })
    },
    reset() {
      this.form.classification = ''
      this.form.desc = ''
      this.form.invitationTypeId = ''
    },
    cancel() {
      this.dialogFormVisible = false
      this.$nextTick(() => {
        this.reset()
        this.$refs.ruleForm.resetFields()
      })
    },
    // 查询
    search() {
      this.currentPage = 1
      // console.log(this.formData.name1)
      this.getClassificationList(undefined, this.formData.name1)
    }
  }
}
</script>

<style lang="scss" scoped>
// dialog框header背景色
::v-deep .el-dialog__header {
  background: #0480fb;
}
// 设置dialog框叉号颜色
::v-deep .el-dialog__close {
  color: #fff;
}
// dialog框标题颜色
::v-deep .el-dialog__title {
  color: #fff;
}
</style>

